/**
 * AppStartViewPager.java
 * classes:com.selfimpr.app.AppStartViewPager
 * 鐠х數顩寸捄锟紺reate at 2013-5-22 娑撳锟�?:52:30
 */
package com.mibao.adapter;

import java.util.ArrayList;
import java.util.List;

import com.mipai.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;


/**
 * 
 * @author xieyinchao
 * 实现引导滑动页面，介绍产品功能，给用户第一视觉体验。
 */
public class AppStartViewPager extends Activity implements OnPageChangeListener {
	private ViewPagerAdapter adapter;
	private ViewPager pager;
	private List<View> views;
	private int[] imageVeiwResourceId = { R.drawable.wizard1, R.drawable.wizard2, R.drawable.wizard3 };
	private ImageView[] point;
	private int currentId = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewpager_start);

		initView();
		setPoint();
	}

	private void initView() {
		pager = (ViewPager) this.findViewById(R.id.vp);
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		views = new ArrayList<View>();

		for (int i = 0; i < imageVeiwResourceId.length; i++) {
			ImageView imageView = new ImageView(this);
			imageView.setImageResource(imageVeiwResourceId[i]);
			imageView.setScaleType(ScaleType.FIT_XY);
			views.add(imageView);
		}

		View view = inflater.inflate(R.layout.last_guide, null);
		views.add(view);
		adapter = new ViewPagerAdapter(views, AppStartViewPager.this);
		pager.setAdapter(adapter);
		pager.setOnPageChangeListener(this);
	}

	private void setPoint() {
		LinearLayout ll = (LinearLayout) this.findViewById(R.id.viewpager_ll);
		point = new ImageView[ll.getChildCount()];
		for (int i = 0; i < ll.getChildCount(); i++) {
			if (currentId == i) {
				point[i] = (ImageView) ll.getChildAt(i);
				point[i].setImageResource(R.drawable.point_focus);
			} else {
				point[i] = (ImageView) ll.getChildAt(i);
				point[i].setImageResource(R.drawable.point_normal);
			}

		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {}

	@Override
	public void onPageSelected(int arg0) {
		currentId = arg0;
		setPoint();

	}
}
